JBoss Community Archive (Read Only)

Infinispan 5.1

Using the Cache API

The Cache interface

Infinispan exposes a simple, JSR-107 compliant Cache interface.

images/community.jboss.org/servlet/JiveServlet/downloadImage/2133/cache_api.png

The Cache interface exposes simple methods for adding, retrieving and removing entries, including atomic mechanisms exposed by the JDK's ConcurrentMap interface.  Based on the cache mode used, invoking these methods will trigger a number of things to happen, potentially even including replicating an entry to a remote node or looking up an entry from a remote node, or potentially a cache store.

Note: For simple usage, using the Cache API should be no different from using the JDK Map API, and hence migrating from simple in-memory caches based on a Map to Infinispan's Cache should be trivial.

Limitations of Certain Map Methods

Certain methods exposed in Map have certain limitations when used with Infinispan, such as size(), values(), keySet() and entrySet().  Specifically, these methods are unreliable and only provide a best-effort guess.  They do not acquire locks, either local or global, and concurrent modifications, additions and removals will not be considered in the result of any of these calls.  Further, they only operate on the local data container, and as such, do not give you a global view of state.

Attempting to perform these operations globally would have large performance impact as well as become a scalability bottleneck.  As such, these methods should only be used for informational or debugging purposes only.

Mortal and Immortal Data

Further to simply storing entries, Infinispan's cache API allows you to attach mortality information to data.  For example, simply using put(key, value) would create an immortal entry, i.e., an entry that lives in the cache forever, until it is removed (or evicted from memory to prevent running out of memory).  If, however, you put data in the cache using put(key, value, lifespan, timeunit), this creates a mortal entry, i.e., an entry that has a fixed lifespan and expires after that lifespan.

In addition to lifespan , Infinispan also supports maxIdle as an additional metric with which to determine expiration.  Any combination of lifespans or maxIdles can be used.

Example of Using Expiry and Mortal Data

See this page for an example of using mortal data with Infinispan

putForExternalRead operation

Infinispan's Cache class contains a peculiar 'put' operation called putForExternalRead. This operation is particularly useful when Infinispan is used as cache to store data that's frequently read, which originates in another data store. The aim of this operation is to deal with concurrent storage of data, which originates in the data store, in the cache in the most efficient way. To achieve this, putForExternalRead is used as a put operation that is fast, only operates if the key is not present in the cache, and fails fast and silently if another thread is trying to store the same key at the same time.

To understand how to use this operation, let's look at basic example. Imagine a cache of Person instances, each keyed by a PersonId, whose data originates in a separate data store. The following code shows the most common pattern of using putForExternalRead within the context of this example:

// Id of the person to look up, provided by the application
PersonId id = ...;

// Get a reference to the cache where person instances will be stored
Cache<PersonId, Person> cache = ...;

// First, check whether the cache contains the person instance
// associated with with the given id
Person cachedPerson = cache.get(id);

if (cachedPerson == null) {
   // The person is not cached yet, so query the data store with the id
   Person person = dataStore.lookup(id);

   // Cache the person along with the id so that future requests can
   // retrieve it from memory rather than going to the data store
   cache.putForExternalRead(id, person);
} else {
   // The person was found in the cache, so return it to the application
   return cachedPerson;
}

Please note that putForExternalRead should never be used as a mechanism to update the cache with a new Person instance originating from application execution (i.e. from a transaction that modifies a Person's address). When updating cached values, please use the standard put operation, otherwise the possibility of caching corrupt data is likely.

The AdvancedCache interface

In addition to the simple Cache interface, Infinispan offers an AdvancedCache interface, geared towards extension authors.  The AdvancedCache offers the ability to inject custom interceptors, access certain internal components and to apply flags to alter the default behavior of certain cache methods.  The following code snippet depicts how an AdvancedCache can be obtained:

AdvancedCache advancedCache = cache.getAdvancedCache();

Flags

Flags are applied to regular cache methods to alter the behavior of certain methods.  For a list of all available flags, and their effects, see the Flag enumeration.  Flags are applied using AdvancedCache.withFlags().  This builder method can be used to apply any number of flags to a cache invocation, for example:

advancedCache.withFlags(Flag.CACHE_MODE_LOCAL, Flag.SKIP_LOCKING)
   .withFlags(Flag.FORCE_SYNCHRONOUS)
   .put("hello", "world");

Custom Interceptors

The AdvancedCache interface also offers advanced developers a mechanism with which to attach custom interceptors.  Custom interceptors allow developers to alter the behavior of the cache API methods, and the AdvancedCache interface allows developers to attach these interceptors programmatically, at run-time.  See the AdvancedCache Javadocs for more details.

For more information on writing custom interceptors, see /javascript:;

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-11 09:16:45 UTC, last content change 2012-01-16 11:10:16 UTC.